package com.ziguang.redwood.web.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;


public class ProductCenterService {

	private static final Logger log = LoggerFactory.getLogger(ProductCenterService.class);

	public static final ProductCenterService me = new ProductCenterService();
	
	
	/**
	 * 获取产品信息
	 */
	public List<Record> findProduct(String typeId,Integer num){
		num = num - 1;
		List<Record> productList = null;
		if(typeId != null){
			productList = Db.find("select * from rw_product where type_id = ? order by sort limit ?,?",typeId,num*15,15);
		}else{
			productList = Db.find("select * from rw_product order by sort limit ?,?",num*15,15);
		}
		return productList;
	}
	
	/**
	 * 获取产品页数
	 */
	public Record findCount(String typeId){
		Record count = null;
		if(typeId != null){
			count = Db.findFirst("select CEIL(count(1)/12) as Pcount from rw_product where type_id = ?",typeId);
		}else{
			count = Db.findFirst("select CEIL(count(1)/12) as Pcount from rw_product");
		}
		return count;
	}
	
	/**
	 * 获取产品分类
	 */
	public List<Record> findProductType(){
		List<Record> ProductTypeList = Db.find("select * from rw_product_type order by sort");
		return ProductTypeList;
	}
	
	/**
	 * 获取产品详情
	 */
	public Record findProductDetail(String id){
		Record detail = Db.findFirst
				("select p.*,t.type_name from rw_product p left join rw_product_type t on p.type_id = t.id where p.id = ?",id);
		return detail;
	}
	
}
